****************************************************************
****************************************************************
*REPLICATION FILE **********************************************
*"Buying Evidence: Policy Research as a Presidential Commodity"*
*Journal of Politics *******************************************
*Rachel Augustine Potter ***************************************
*rapotter@virginia.edu *****************************************
*December 2023 **************************************************
****************************************************************
****************************************************************

**This .do files compiles the various files together into a master dataset and codes several variables generated

*Before beginning, change the working directory to your local directory 
cd "Buying Evidence Replication/"

*********************************
*********************************
use "research_contracts.dta"

*Add political environment data
joinby year using "politicalenv.dta", unmatched(master)
drop _merge

*Add discretionary budget authority data
joinby OPMdept year using "budget_auth.dta", unmatched(master)
drop _merge

*Add subtopic Policy Agendas data
joinby PA_subtopic year using "PAgendas_subtopic_year.dta", unmatched(master)
drop _merge

*Add agency personnel data
joinby OPMagency year using "personnel.dta", unmatched(master)
drop _merge
replace num_fulltime = num_fulltime / 100
gen contractp100 = contracting_employees/num_fulltime

*Add presidential particularism extension data
joinby state_po year using "swing.core.dta", unmatched(master)
drop _merge

*Add congressional demand extension data
joinby PA_subtopic year month using "bills.dta", unmatched(master)
drop _merge

*********************************
*********************************
*Code key variables

sort award_id date
by award_id: gen modnumber = _n 
egen minmod = min(modnumber), by(award_id)
egen maxmod = max(modnumber),  by(award_id)

gen date_init = date if modnumber == minmod
bysort award_id: carryforward date_init, gen(yn)
drop date_init
rename yn date_init
format date_init %td

gen initialadmin = 1 if  minmod == modnumber & clinton ==1
replace initialadmin = 2 if  minmod == modnumber  & bushii ==1
replace initialadmin = 3 if minmod == modnumber  & obama ==1
replace initialadmin = 4 if  minmod == modnumber  & trump ==1
bysort  award_id: carryforward initialadmin, gen(initialadmin2)
gen terminateadmin = 1 if  maxmod == modnumber & clinton ==1 & amount !=.
replace terminateadmin = 2 if maxmod == modnumber & bushii ==1 & amount !=.
replace terminateadmin = 3 if maxmod == modnumber & obama ==1 & amount !=.
replace terminateadmin = 4 if maxmod == modnumber & trump ==1 & amount !=.
gen newadmin = 1 if terminateadmin != initialadmin2  & maxmod == modnumber
replace newadmin = 0 if terminateadmin == initialadmin2 & maxmod == modnumber
drop minmod date_init initialadmin2 terminateadmin

gen samenow =0
replace samenow = 1 if clinton ==1 & initialadmin ==1
replace samenow = 1 if bushii ==1 & initialadmin ==2
replace samenow = 1 if obama ==1 & initialadmin ==3
replace samenow = 1 if trump ==1 & initialadmin ==4

encode FPDS, gen(Agency)
gen restricted = 1 if inhousecapacity != .  & budgetauth_pc !=.   & contractp100 !=. 
gen restricted2 = 1 if lnawardtotal != .  & restricted ==1 
